<template>
  <div class="ma-content-block  p-4">
    <!-- CRUD 组件 -->
    
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #operationBeforeExtend="{ record }">
          <a-link @click="show(record)"  :disabled='record.status == 0?false:true' v-auth="['financial:transactions:check']">审核</a-link>
      </template>
      <template #price="{ record }">
            <div>{{record.price.toFixed(2)}}</div>
      </template>
      <template #summaryCell="{ column, record, rowIndex }">
          {{
              column.dataIndex == "dept_id" ? "小计" : column.dataIndex == "price" ? (record[column.dataIndex] - 0).toFixed(2) : record[column.dataIndex]
          }}
      </template>
    </ma-crud>
    <div>合计：{{heji}}</div>
    <!-- :optionsCrud="optionsCrud" :columnsCrud="columnsCrud" -->
    <ma-form-model :infoColumns="columns" :columns="checkColumns"  title="审核订单" ref="showRef" :submit="checkSubmit">
      
    </ma-form-model>
    <a-modal :mask-closable="false"  v-model:visible="showCrud" :on-before-ok="handleOk" @cancel="handleCancel">
      <template #title>
        确认审核
      </template>
      <div style='text-align:center'>自动审核开启后，【同提单下的装车单都将以现结-对公转账的方式默认支付，后续无需再次审核】，是否确认？</div>
    </a-modal>
  </div>
</template>
<script setup>
import { ref, reactive,onActivated } from 'vue'
import { request } from "@/utils/request.js";
import financialTransactions from '@/api/financial/financialTransactions'
import MaFormModel from "@/components/ma-form-modal/index.vue"
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const showRef = ref()
const crudRef = ref()
const showCrud = ref(false)
const shehedata = ref({})
const shuaxinShow = ref(false)
const heji = ref(0)
const indexid = ref(0)

const open =  (row) => {
  indexid.value = row.user_id
  crudRef.value.refresh()
}
onActivated(()=>{
  if(shuaxinShow.value){
    crudRef.value.refresh()
  }
})
const checkColumns = [
  {
    title: "审核状态",
    dataIndex:"check_status",
    formType: "radio",
    dict: {
      data: [
        {label: "成功", value: 1},
        {label: "驳回", value: 0},
      ]
    },
    rules: {
      required: true,
      message: "请选择审核状态",
    }
  },
  // {
  //   title: "自动审核",
  //   dataIndex: "is_auto_check",
  //   formType: "switch",
  //   hide:true,
  // },
  {
    title: "备注",
    dataIndex: "notes",
    formType: "textarea"
  }
];
const handleCancel = ()=>{
  showCrud.value = false
}
const handleOk = async ()=>{
  let res = await request({
    url: 'financial/transactions/check',
    method: 'post',
    data:shehedata.value,
  })
  if(res.code == 200){
    Message.success(res.message)
    showCrud.value = false
    crudRef.value.refresh()
    return true
  }else{
    Message.error(res.message)
    return false
  }
  // request({
  //     url: 'financial/transactions/check',
  //     method: 'post',
  //     data:shehedata.value,
  //   }).then(res=>{
  //     if (res.success) {
        
  //     }
  //   })
}
const checkSubmit = async (data) => {
  shehedata.value = data
  if(data.check_status == 1){
    data.is_auto_check = data.is_auto_check?1:0
  }else{
    data.is_auto_check = 0
  }
  if(data.is_auto_check == 1){
    showCrud.value = true
  }else{
    request({
      url: 'financial/transactions/check',
      method: 'post',
      data,
    }).then(res=>{
      if (res.success) {
        Message.success(res.message)
        crudRef.value.refresh()
        return true
      }
    })
  }
  
}

const show = (row) => {
  showRef.value.open({id: row.id}, row)
}

const options = reactive({
  id: 'flyos_financial_transactions',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: false,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  showSummary: true,
  summary: [
        // 定义 amountA 字段加总合计
        {
            dataIndex: 'price',
            action: 'sum'
        },
    ],
  api: financialTransactions.getList,
  add: {
    show: false,
    api: financialTransactions.save,
    auth: ['financial:transactions:save']
  },
  edit: {
    show: false,
    api: financialTransactions.update,
    auth: ['financial:transactions:update']
  },
  delete: {
    show: false,
    api: financialTransactions.deletes,
    auth: ['financial:transactions:delete']
  },
  export: {
    show: true,
    url: 'financial/transactions/export',
    auth: ['financial:transactions:export']
  },
  beforeRequest: (params) => {
    params.dept_id = indexid.value
    console.log(indexid)
        request({
          url:'financial/transactions/total',
          method: 'get',
          params,
        }).then(res=>{
          heji.value = res.data.total
        })
	},
  afterRequest: (params) => {
    console.log(indexid)
    
		params.map((v, i) => {
      if(v.transfer_proof_images != null){
        v.transfer_proof_images = v.transfer_proof_images.split(',')
      }
      v.account = v.account || "-"
      v.opening_bank = v.opening_bank || "-"
      v.price = v.price - 0
      if(v.type == 2){
        v.order = {pay_price:v.price,prediction_order_number:'-',order_number:'-'}
      }
		});
    shuaxinShow.value = true
	},
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    infoShow: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  
  {
    title: "公司",
    dataIndex: "dept_id",
    width:180,
    align:'center',
    common: true,
    search: false
  },
  {
    title: "结算方式",
    dataIndex: "settlement_method",
    formType: "select",
    search: false,
    align:'center',
    dict: {
      data: [
        {
          label: "现结",
          value: "1"
        }
      ],
      translation: true
    }
  },
  {
    title: "支付方式",
    dataIndex: "payment_method",
    formType: "select",
    search: false,
    align:'center',
    width:100,
    dict: {
      data: [
        {
          label: "对公转账",
          value: "3"
        },
        {
          label: "现金",
          value: "2"
        },
        {
          label: "惠支付",
          value: "1"
        }
      ],
      translation: true
    }
  },
  // {
  //   title: "开户行名称",
  //   dataIndex: "account_name",
  //   formType: "input",
  //   search: true
  // },
  {
    title: "交易类型",
    dataIndex: "type",
    formType: "select",
    width:100,
    align:'center',
    dict: {
      data: [
        {
          label: "账单支付",
          value: "1"
        },
        {
          label: "充值",
          value: "2"
        },
      ],
      translation: true
    }
  },
  
  
  {
    title: "交易金额",
    dataIndex: "price",
    align:'center',
    width:140,
    search: false,
  },
  {
    title: "审核人",
    dataIndex: "checkuser.nickname",
    align:'center',
  },
  {
    title: "审核状态",
    dataIndex: "status",
    align:'center',
    formType: "radio",
    dict: {
      data()
      {
        return [
          {
            label: "审核成功",
            value: 1
          },
          {
            label: "已驳回",
            value: 2
          },
          {
            label: "等待审核",
            value: 0
          },
        ]
      },
      translation: true,
    }
  },
  {
    title: "审核日期",
    dataIndex: "check_date",
    align:'center',
    width:180,
  },
  {
    title: "提交人",
    dataIndex: "createuser.nickname",
    align:'center',
  },
  {
    title: "提交时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    width:180,
    editDisplay: false,
    showTime: true
  },
  {
    title: "转账凭证",
    dataIndex: "transfer_proof_images",
    formType: "upload",
    align:'center',
    width:140,
    type: "image",
    multiple: true
  },
  {
    title: "收款账户",
    dataIndex: "account",
    align:'center',
    formType: "input",
    search: true
  },
 
  {
    title: "开户行",
    dataIndex: "opening_bank",
    formType: "input",
    align:'center',
    search: true
  },
  {
    title: "提货单号",
    dataIndex: "order.prediction_order_number",
    formType: "input",
    align:'center',
    width:140,
    search: true
  },
 
  {
    title: "交易单号",
    dataIndex: "order.order_number",
    align:'center',
    formType: "input",
    width:140,
    search: true
  },
  {
    title: "备注",
    dataIndex: "notes",
    formType: "editor",
    align:'center',
  },
  {
    title: "修改时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  // {
  //   title: "删除时间",
  //   dataIndex: "deleted_at",
  //   formType: "date",
  //   addDisplay: false,
  //   editDisplay: false,
  //   hide: true,
  //   showTime: true
  // },
  // {
  //   title: "创建者",
  //   dataIndex: "created_by",
  //   formType: "input",
  //   addDisplay: false,
  //   editDisplay: false,
  //   hide: true
  // },
  // {
  //   title: "更新者",
  //   dataIndex: "updated_by",
  //   formType: "input",
  //   addDisplay: false,
  //   editDisplay: false,
  //   hide: true
  // }
])
defineExpose({ open });
</script>
<script> export default { name: 'financial:transactions' } </script>
